local Array =import("app.engine.struct.Array")
local Layouts = import("..others.Layouts")
import("app.scenes.mainScene.controller.ViewController")
local AnimationLayer =class("AnimationLayer",function() return  display.newLayer() end)
function AnimationLayer:ctor()
     
     local fg =self:createFg()
     fg:setZOrder(1)
     display.align(fg,display.LEFT_BOTTOM,0,0)
     self:addChild(fg)
     
     self.fgWidth =4118
     self.fgX =self.fgWidth-1
     self.fgList =Array.new()
     self.fgList:push(fg)
     
     -- Layouts
    self.layouts =Layouts.new()
     
     
end

function AnimationLayer:createFg()
   local sprite =CCSprite:create()
   local fg1 =display.newSprite("fg1.png")
   local fg2 =display.newSprite("fg2.png")
   local fg3 =display.newSprite("fg3.png")
   local fg4 =display.newSprite("fg4.png")
   local fg5 =display.newSprite("fg5.png")
   sprite:addChild(fg1)
   sprite:addChild(fg2)
   sprite:addChild(fg3)
   sprite:addChild(fg4)
   sprite:addChild(fg5)
   local x=0
   display.align(fg1,display.LEFT_BOTTOM,x,0)
   x =x+fg1:getContentSize().width
   display.align(fg2,display.LEFT_BOTTOM,x,0)
   x =x+fg2:getContentSize().width
   display.align(fg3,display.LEFT_BOTTOM,x,0)
   x =x+fg3:getContentSize().width
   display.align(fg4,display.LEFT_BOTTOM,x,0)
   x =x+fg4:getContentSize().width
   display.align(fg5,display.LEFT_BOTTOM,x,0)
   return sprite
   
end



-- 1s
function AnimationLayer:onUpdate()
--   print("Animation onUpdate")
   local px,py =self:getPosition()
   local ui =self.layouts:getRandomUI(px,0)
   if ui then self:addChild(ui) end
   if self.fgList:getSize()<=5 then   
     local fg =self:createFg()
     display.align(fg,display.LEFT_BOTTOM,self.fgX,0)
     self:addChild(fg)
     fg:setZOrder(1)
     self.fgX=self.fgX+self.fgWidth-1
     self.fgList:push(fg)
   end 
   self:checkRemoveFg()
end




function AnimationLayer:isColliWithFish(fishRect)
   return  self.layouts:isColliWithFish(fishRect)
end

function AnimationLayer:colliCheckOther(fishRect)
     self.layouts:colliCheckWithCoin(fishRect)
     self.layouts:colliCheckWithShaYu(fishRect)
end

function AnimationLayer:checkColliWithDaoJu(fishRect)
 self.layouts:checkColliWithDaoJu(fishRect)
end

function AnimationLayer:collWithDaoJu()

end

function AnimationLayer:passOne(fishRect)
 return self.layouts:passOne(fishRect) 
end

function AnimationLayer:checkRemoveFg()
   local px,py =self:getPosition()
   for i=0,self.fgList:getSize()-1 do
    local fgObj =self.fgList:get(i)
    if  fgObj then
      local fgX,fgY =fgObj:getPosition()
      if fgX+px+self.fgWidth<0 then
          self:removeChild(fgObj)
          self.fgList:remove(i)
          print("remove fgX at",i)
      end
    end    
   end

end

return AnimationLayer
